#include "wscaircraftkmlwaypoints.h"
using namespace FsxWebService;

//
// Constructor
//
WscAircraftKmlWaypoints::WscAircraftKmlWaypoints(const KmlSettings &kmlSettings, const int port, const QString &serverUrl, const QString &path, QObject *parent) :
    WscRequestHandler(port, serverUrl, path, parent), _kmlSettings(kmlSettings)
{
    // constructor code
}

//
// Service of the web service
//
void WscAircraftKmlWaypoints::service(HttpRequest& request, HttpResponse& response) {

    // set some headers
    response.setHeader("Content-Type", "application/kml; charset=ISO-8859-1");
    response.setCookie(HttpCookie("wscaircrafts","aircrafts",600));

    // generate response
    int id;
    int waypointResolutionMs, waypointsMax;
    bool reloading;
    WscAircraftKmlWaypoints::getParameters(request, id, waypointResolutionMs , waypointsMax ,reloading);
    if (reloading) {
        // KmlNetworkLink loading and refresing the content
        QString url = HttpHelper::removeQueryString(request.getPath());
        QString description= request.getPath();
        description.replace(QRegExp(".+/"),"").replace(".kml","");
        description += (" (max.wp: " + QString::number(WPMAXNETWORKLINK) + ")");
        url = HttpHelper::appendToUrl(WscAircraftKmlWaypoints::getServerUrl(), url);
        url += ("?id=" + QString::number(id));
        url += "&amp;"; // since this goes to an XML document, I need to encode &
        url += ("waypointresolution=" + QString::number(waypointResolutionMs));
        url += "&amp;";
        url += ("waypointsmax=" + QString::number(WPMAXNETWORKLINK)); // due to the Google Earth bug loading large data in NetworkLink
        QString networkLink = JsonMarkupHelper::kmlNetworkLinkDocument(url, description, WscAircraftKmlWaypoints::_kmlSettings.networkLinkUpdatePeriodS);
        QByteArray ba = networkLink.toLocal8Bit();
        response.write(ba);
    } else {
        // the contents itself
        KmlParameter parameter(WscAircraftKmlWaypoints::_kmlSettings);
        parameter.id = id;
        parameter.waypointResolutionPeriodMs = waypointResolutionMs;
        parameter.maxNumberWaypoints = waypointsMax;
        QString dp = emit WscAircraftKmlWaypoints::signalAircraftKmlWaypoints(parameter);
        if (dp.length() < 1) {
            response.write("");
        } else {
            QByteArray ba = dp.toLocal8Bit();
            response.write(ba);
        }
    }
}

//
// Get relevant parameters from the request
//
void WscAircraftKmlWaypoints::getParameters(HttpRequest& request, int &id, int &waypointResolutionMs, int &waypointsMax, bool &reloading) {
    // id
    QString pId = QString(request.getParameter("id"));
    bool validId = !pId.isEmpty() && (id=pId.toInt(&validId));
    // resolution
    QString res = QString(request.getParameter("waypointresolution"));
    bool validRes = !res.isEmpty() && (waypointResolutionMs=res.toInt(&validRes));
    // max waypoints
    QString maxwp = QString(request.getParameter("waypointresolution"));
    bool validMax = !maxwp.isEmpty() && (waypointsMax=res.toInt(&validMax));
    // reloading
    reloading = HttpHelper::parameterToBool(request.getParameter("reload"));

    // reset invalid values
    if (!validId) id = 1; // own aircraft
    if (!validMax) waypointsMax = -1;
    if (!validRes) waypointResolutionMs = -1;
}
